package com.ruoyi.tjfx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.controller.TestExcelController;
import com.ruoyi.syrw.domain.bo.BsDataReportBo;
import com.ruoyi.syrw.domain.bo.TaiZhangQueryBo;
import com.ruoyi.syrw.domain.vo.BsDataReportVo;
import com.ruoyi.syrw.domain.vo.TaiZhangVo;
import com.ruoyi.syrw.service.IBsDataReportService;
import com.ruoyi.tjfx.service.ReportTJFXService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * tong_ji_fen_xi
 *
 * @author zyb
 * @date 2024-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tjfx")
public class ReportTJFXController extends BaseController {

    private final ReportTJFXService reportTJFXService;
    private final IBsDataReportService iBsDataReportService;

    /**
     * 根据syTypeId和deptId查询试验任务台账
     */
    @SaCheckLogin
    @GetMapping("/taiZhang")
    public TableDataInfo taiZhang(@Validated(QueryGroup.class)TaiZhangQueryBo bo, PageQuery pageQuery) {
        return reportTJFXService.taiZhang(bo, pageQuery);
    }

    /**
     *  根据syTypeId和deptId导出试验台账
     */
    @SaCheckLogin
    @PostMapping("/exportTz")
    public void exportBhgtz(@Validated(QueryGroup.class)TaiZhangQueryBo bo , HttpServletResponse response) {
        TableDataInfo tableDataInfo = reportTJFXService.taiZhang(bo, new PageQuery());
        Map<String, String> map = new HashMap<>();
        // 台账
        List rows = tableDataInfo.getRows();
        String tamplatePath = getTzTemplatePath(bo);
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map, rows), "试验台账.xlsx", tamplatePath, response);
    }

    private String getTzTemplatePath(TaiZhangQueryBo bo) {
        if (bo.getSyType() == 2L) {
            return "excel/土工试验台账.xlsx";
        } else if (bo.getSyType() == 4L) {
            return "excel/粗集料试验台账.xlsx";
        } else if (bo.getSyType() == 5L) {
            return "excel/细集料试验台账.xlsx";
        } //  钢筋原材试验 复制粗集料修改 lb
        else if (25L == bo.getSyType()) {
            return "excel/钢筋原材试验台账.xlsx";
        }
        //  钢筋焊接试验 复制粗集料修改 lb
        else if (136L == bo.getSyType()) {
            return "excel/钢筋焊接试验台账.xlsx";
        }
        // 钢筋机械连接试验 复制粗集料修改 lb
        else if (137L == bo.getSyType()) {
            return "excel/钢筋机械连接验台账.xlsx";
        }
        // 粉煤灰试验（掺合料） 复制粗集料修改 zyb
        else if (22L == bo.getSyType()) {
            return "excel/粉煤灰试验（掺合料）试验台账.xlsx";
        }
        //  水泥 lb
        else if (10L == bo.getSyType()) {
            return "excel/水泥试验台账.xlsx";
        }
        // todo 水泥混凝土配合比  xs
        else if (14L == bo.getSyType()) {
            return "excel/水泥混凝土配合比试验台账.xlsx";
        }
        // todo  水泥砂浆配合比试验  xs
        else if (17L == bo.getSyType()) {
            return "excel/水泥砂浆配合比试验台账.xlsx";
        }
        // todo 沥青混合料配合比  lb
        else if (139L == bo.getSyType()) {
            return "excel/沥青混合料配合比试验台账.xlsx";
        }
        // 基坑、地基与桩基试验  zyb
        else if (31L == bo.getSyType()) {
            return "excel/基坑、地基与桩基试验台账.xlsx";
        }
        // 压浆材料 lb
        else if (69L == bo.getSyType()) {
            return "excel/压浆材料试验台账.xlsx";
        }
        // 路基路面回弹弯沉试验 lb
        else if (135L == bo.getSyType()) {
            return "excel/路基路面试验台账.xlsx";
        }
        return "excel/未知试验类型台账.xlsx";
    }

    /**
     * 不合格台账
     */
    @SaCheckLogin
    @GetMapping("/bhgtz")
    public TableDataInfo bhgtz(BsDataReportBo bo, PageQuery pageQuery) {
        return reportTJFXService.bhgtz(bo, pageQuery);
    }


    /**
     * 导出不合格台账
     */
    @SaCheckLogin
    @PostMapping("/exportBhgtz")
    public void exportBhgtz(BsDataReportBo bo , HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        // 不合格台账
        bo.setReportResult(2);
        List<BsDataReportVo> bsDataReportVos = iBsDataReportService.queryList(bo);
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map, bsDataReportVos), "不合格台账.xlsx", "excel/不合格台账.xlsx", response);
    }



}
